package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.MarketCommodity;
import com.woniuxy.common.entity.MarketTalk;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.MarketCommodityMapper;
import com.woniuxy.common.mapper.MarketTalkMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.MarketTalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MarketTalkServiceImpl implements MarketTalkService {
    @Autowired
    MarketTalkMapper marketTalkMapper;

    @Autowired
    MarketCommodityMapper marketCommodityMapper;

    @Autowired
    CenterUserMapper centerUserMapper;

    @Override
    public List<MarketTalk> findAllTalksByInfo(String loginuserid, String commodityid, String userid) {
        List<MarketTalk> marketTalks = marketTalkMapper.selectAllTalksByInfo(loginuserid, commodityid, userid);
        for (MarketTalk marketTalk:marketTalks) {
            MarketCommodity commodity = marketCommodityMapper.selectByPrimaryKey(marketTalk.getCommodityId());
            CenterUser loginuser = centerUserMapper.selectByPrimaryKey(loginuserid);
            CenterUser user = centerUserMapper.selectByPrimaryKey(userid);
            if(marketTalk.getReceiveuserId()==loginuserid){
                marketTalk.setReceiveUser(loginuser);
                marketTalk.setSendUser(user);
            }else if(marketTalk.getSenduserId()==loginuserid){
                marketTalk.setSendUser(loginuser);
                marketTalk.setReceiveUser(user);
            }
        }
        return marketTalks;
    }

    @Override
    public int insertTalkByInfo(String loginuserid, String commodityid, String userid,String talk) {
        MarketTalk marketTalk=new MarketTalk();
        marketTalk.setId(KeyUtil.generateKey());
        marketTalk.setSenduserId(loginuserid);
        marketTalk.setReceiveuserId(userid);
        marketTalk.setCommodityId(commodityid);
        marketTalk.setTalk(talk);
        marketTalk.setDate(new Date());
        int insert = marketTalkMapper.insert(marketTalk);
        return insert;
    }
}
